import * as mutationtypes from '../mutation-types';
/* import * as mutationtypes from './mutation-types'; */
import {Utils} from '@/utils/utils.js';
import Vue from 'vue';
// 初始化首屏数据
export const initGlobalData = ({dispatch, commit}, authdata) => {
  commit('BEGIN_LOADING');
  return Vue.axios.post('/Jwt/GetJwt/WeChatMall', authdata).then((response) => {
    if (response.data.isSuccess) {
      // 保存JWT 到cookie
      Utils.SetJwt(response.data.token);
      Vue.axios.get('/WechatMall/Auth/CheckJwt').then((response) => {
        if (response.data.isSuccess) {
          dispatch('refreshShops').then(() => {
            commit(mutationtypes.END_LOADING);
            commit(mutationtypes.INIT_COMPLETED);
          });
        }
      });
    }
  });
};

const app = {
  state: {isLoading: true, isNavbarVis: true, InitDataCompleted: false, AuthData: {}},
  mutations: {
    [mutationtypes.BEGIN_LOADING](state) {
      state.isLoading = true;
    },
    [mutationtypes.END_LOADING](state) {
      state.isLoading = false;
    },
    [mutationtypes.HIDE_NAVBAR](state) {
      state.isNavbarVis = false;
    },
    [mutationtypes.VIS_NAVBAR](state) {
      state.isNavbarVis = true;
    },
    [mutationtypes.INIT_COMPLETED](state) {
      state.InitDataCompleted = true;
    },
    [mutationtypes.SET_AUTHDATA](state, authdata) {
      state.AuthData = authdata;
    }
  },
  actions: {initGlobalData}
};

export default app;
